package com.axon.components.controls
{
	import com.axon.components.containers.AxPanel;
	import com.axon.components.containers.AxTitleWindowSizeable;
	import com.axon.components.events.AxListEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Tree;
	import mx.events.ListEvent;
	
	import org.restfulx.collections.ModelsCollection;
	import org.restfulx.models.RxModel;

	[Event(name="axChange",		 		type="com.axon.components.events.AxListEvent")]  // name must be equal to AxonListEvent.SELECTION_CHANGE 
	[Event(name="axDefaultGesture", 	type="com.axon.components.events.AxListEvent")]  // name must be equal to AxonListEvent.DEFAULT_GESTURE

	public class AxTree extends Tree
	{
		private var fAxLastSelectedId		: String 	= "";

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
   		[Inspectable(category="Other")] public var axAllowNodeExpansion  : Boolean = true;
   		[Inspectable(category="Other")] public var axHandleDefaultAction : Boolean = true;
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function AxTree()
		{
			super();
			addEventListener( ListEvent.CHANGE, 			onChange );
			addEventListener( KeyboardEvent.KEY_DOWN, 		onKeyDown);
			addEventListener( MouseEvent.DOUBLE_CLICK, 		onDoubleClick);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function memorizeLastSelection(): void
		{
			if ((selectedItem) && (dataProvider is ModelsCollection)) 
				fAxLastSelectedId = (RxModel(selectedItem)).id;

			dispatchEvent(new AxListEvent(AxListEvent.SELECTION_CHANGE));
		} 			
	
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set dataProvider(value: Object): void
		{
			if (value == dataProvider)
				return;
			
			super.dataProvider = value;
			dispatchEvent(new AxListEvent(AxListEvent.SELECTION_CHANGE));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function onChange(event: Event): void
		{
			dispatchEvent(new AxListEvent(AxListEvent.SELECTION_CHANGE));
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function onKeyDown(event: KeyboardEvent): void
		{
			var theParent: DisplayObjectContainer;
			
			//  Escape
			
			if (event.charCode == Keyboard.ESCAPE)
			{
				theParent = parent;
				
				while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
					theParent = theParent.parent;
					
				if (theParent != null) 
				theParent.dispatchEvent(event.clone());
			}
			
			// Space
			 
			else if ((event.charCode == Keyboard.SPACE) && selectedItem)
			{
				axPerformDefaultGesture();
			}   
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function onDoubleClick(event: MouseEvent): void
		{
			axPerformDefaultGesture();
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set selectedItem(value: Object): void
		{
			if (value == selectedItem)
				return;
			
			super.selectedItem = value;
			memorizeLastSelection();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set selectedIndex(value: int): void
		{
			if (value == selectedIndex)
				return;
				
			super.selectedIndex = value;
			memorizeLastSelection();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function get axLastSelectedId(): String
		{
			return fAxLastSelectedId;
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function axPerformDefaultGesture(): void
		{
			if ( selectedItem )
				dispatchEvent(new AxListEvent(AxListEvent.DEFAULT_GESTURE));
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function axSelectItemWithId(id: String): int
		{
			var index: int = -1;
			
			if (dataProvider is ModelsCollection) {
				index = ModelsCollection(dataProvider).indexOfId(id);
			} else {
				index = -1;
			}
			selectedIndex = index;
			
			return index;
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function axRecallLastSelection(): int
		{
			return axSelectItemWithId(fAxLastSelectedId);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function expandChildrenOf(item: Object, open: Boolean):void
		{
			if (axAllowNodeExpansion)
				super.expandChildrenOf(item, open);
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function expandItem(item: Object, open: Boolean, animate: Boolean = false, 
												dispatchEvent: Boolean = false, cause: Event = null): void
		{
			if (axAllowNodeExpansion)
				super.expandItem(item, open, animate, dispatchEvent, cause);;
		}

	}
}